package com.chookss.mine.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.WebCommonActivity;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.base.Token;
import com.chookss.home.entity.NewsEntiry;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.view.MyImageSpan;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.annotations.Nullable;

public class MyNewsListsAdapter extends BaseQuickAdapter<NewsEntiry, BaseViewHolderEx> {
    private Context context;
    private int height, width;

    public MyNewsListsAdapter(int layoutResId, Context context, @Nullable List<NewsEntiry> data) {
        super(layoutResId, data);
        this.context = context;
        width = (Utils.getScreenWidthPixel(context) - Utils.dip2px(context, 72)) / 3;
        height = width * 65 / 102;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, NewsEntiry item) {
        TextView tvTitle = helper.getView(R.id.tvTitle);
        TextView tvName = helper.getView(R.id.tvName);
        TextView tvDate = helper.getView(R.id.tvDate);
        LinearLayout llOneImg = helper.getView(R.id.llOneImg);
        LinearLayout llThreeImg = helper.getView(R.id.llThreeImg);
        ImageView ivImg = helper.getView(R.id.ivImg);
        ImageView ivPic1 = helper.getView(R.id.ivPic1);
        ImageView ivPic2 = helper.getView(R.id.ivPic2);
        ImageView ivPic3 = helper.getView(R.id.ivPic3);
        View placeholderView = helper.getView(R.id.placeholderView);
        View spaceView = helper.getView(R.id.spaceView);
        //两个view只能同时存在一个
        LinearLayout llContent = helper.getView(R.id.llContent);
        String title = item.getNewsTitle().trim().replace("\r|\n", "");
        LinearLayout.LayoutParams llOneImgParams = (LinearLayout.LayoutParams) llOneImg.getLayoutParams();
        LinearLayout.LayoutParams ivPic1Params = (LinearLayout.LayoutParams) ivPic1.getLayoutParams();
        LinearLayout.LayoutParams ivPic2Params = (LinearLayout.LayoutParams) ivPic2.getLayoutParams();
        LinearLayout.LayoutParams ivPic3Params = (LinearLayout.LayoutParams) ivPic3.getLayoutParams();
        LinearLayout.LayoutParams llContentParams = (LinearLayout.LayoutParams) llContent.getLayoutParams();

        if ("0".equals(item.getNewsAuditStatus())) {
            //审核中
            SpannableString spannableString = new SpannableString("  " + title);
            Drawable drawable = context.getResources().getDrawable(R.drawable.icon_red_shen);
            drawable.setBounds(
                    0,
                    0,
                    Utils.dip2px(context, 12f),
                    Utils.dip2px(context, 12f)
            );
            MyImageSpan imageSpan = new MyImageSpan(drawable);
            spannableString.setSpan(imageSpan, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            tvTitle.setText(spannableString);
        } else if ("2".equals(item.getNewsAuditStatus())) {
            //审核未通过
            SpannableString spannableString = new SpannableString("  " + title);
            Drawable drawable = context.getResources().getDrawable(R.drawable.icon_red_tui);
            drawable.setBounds(
                    0,
                    0,
                    Utils.dip2px(context, 12f),
                    Utils.dip2px(context, 12f)
            );
            MyImageSpan imageSpan = new MyImageSpan(drawable);
            spannableString.setSpan(imageSpan, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            tvTitle.setText(spannableString);
        } else if ("1".equals(item.getIfTop())) {
            //置顶
            SpannableString spannableString = new SpannableString("  " + title);
            Drawable drawable = context.getResources().getDrawable(R.drawable.icon_xw_ding);
            drawable.setBounds(
                    0,
                    0,
                    Utils.dip2px(context, 12f),
                    Utils.dip2px(context, 12f)
            );
            MyImageSpan imageSpan = new MyImageSpan(drawable);
            spannableString.setSpan(imageSpan, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            tvTitle.setText(spannableString);
        } else if ("1".equals(item.getNewsPublishRole())) {
            //官方
            SpannableString spannableString = new SpannableString("  " + title);
            Drawable drawable = context.getResources().getDrawable(R.drawable.icon_xw_guan);
            drawable.setBounds(
                    0,
                    0,
                    Utils.dip2px(context, 12f),
                    Utils.dip2px(context, 12f)
            );
            MyImageSpan imageSpan = new MyImageSpan(drawable);
            spannableString.setSpan(imageSpan, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            tvTitle.setText(spannableString);
        } else {
            tvTitle.setText(title);
        }
        tvName.setText(item.getEmployeeName());
        tvDate.setText(Utils.getAccurateDate(item.getNewsPublishTime()));
        if (item.getPictures().size() > 0) {
            if (item.getPictures().size() < 3) {
                llOneImg.setVisibility(View.VISIBLE);
                llThreeImg.setVisibility(View.GONE);
                llOneImgParams.height = height;
                llOneImgParams.width = width;
                llOneImg.setLayoutParams(llOneImgParams);
                llContentParams.height = height;
                placeholderView.setVisibility(View.VISIBLE);
                spaceView.setVisibility(View.GONE);
                llContent.setLayoutParams(llContentParams);
                if (Utils.isNull(item.getPictures().get(0).getThumbnailPath())) {
                    if (item.getPictures().get(0).getPicturePath().endsWith("gif")) {
                        GlideUtils.loadCustomRoundGif(context, item.getPictures().get(0).getPicturePath(), ivImg, 4);
                    } else {
                        GlideUtils.loadCustomRound(context,item.getPictures().get(0).getPicturePath(), ivImg, 4);
                    }
                } else {
                    if (item.getPictures().get(0).getThumbnailPath().endsWith("gif")) {
                        GlideUtils.loadCustomRoundGif(context,item.getPictures().get(0).getThumbnailPath(), ivImg, 4);
                    } else {
                        GlideUtils.loadCustomRound(context, item.getPictures().get(0).getThumbnailPath(), ivImg, 4);
                    }
                }
            } else {
                llOneImg.setVisibility(View.GONE);
                llThreeImg.setVisibility(View.VISIBLE);
                ivPic1Params.height = height;
                ivPic1Params.width = width;
                ivPic1.setLayoutParams(ivPic1Params);
                ivPic2Params.height = height;
                ivPic2Params.width = width;
                ivPic2.setLayoutParams(ivPic2Params);
                ivPic3Params.height = height;
                ivPic3Params.width = width;
                ivPic3.setLayoutParams(ivPic3Params);
                llContentParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
                placeholderView.setVisibility(View.GONE);
                spaceView.setVisibility(View.VISIBLE);
                llContent.setLayoutParams(llContentParams);
                if (Utils.isNull(item.getPictures().get(0).getThumbnailPath())) {
                    if (item.getPictures().get(0).getPicturePath().endsWith("gif")) {
                        GlideUtils.loadCustomRoundGif(context, item.getPictures().get(0).getPicturePath(), ivPic1, 4);
                    } else {
                        GlideUtils.loadCustomRound(context, item.getPictures().get(0).getPicturePath(), ivPic1, 4);
                    }
                    if (item.getPictures().get(1).getPicturePath().endsWith("gif")) {
                        GlideUtils.loadCustomRoundGif(context,item.getPictures().get(1).getPicturePath(), ivPic2, 4);
                    } else {
                        GlideUtils.loadCustomRound(context, item.getPictures().get(1).getPicturePath(), ivPic2, 4);
                    }
                    if (item.getPictures().get(2).getPicturePath().endsWith("gif")) {
                        GlideUtils.loadCustomRoundGif(context, item.getPictures().get(2).getPicturePath(), ivPic3, 4);
                    } else {
                        GlideUtils.loadCustomRound(context, item.getPictures().get(2).getPicturePath(), ivPic3, 4);
                    }
                } else {
                    if (item.getPictures().get(0).getThumbnailPath().endsWith("gif")) {
                        GlideUtils.loadCustomRoundGif(context, item.getPictures().get(0).getThumbnailPath(), ivPic1, 4);
                    } else {
                        GlideUtils.loadCustomRound(context, item.getPictures().get(0).getThumbnailPath(), ivPic1, 4);
                    }
                    if (item.getPictures().get(1).getThumbnailPath().endsWith("gif")) {
                        GlideUtils.loadCustomRoundGif(context,item.getPictures().get(1).getThumbnailPath(), ivPic2, 4);
                    } else {
                        GlideUtils.loadCustomRound(context, item.getPictures().get(1).getThumbnailPath(), ivPic2, 4);
                    }
                    if (item.getPictures().get(2).getThumbnailPath().endsWith("gif")) {
                        GlideUtils.loadCustomRoundGif(context,item.getPictures().get(2).getThumbnailPath(), ivPic3, 4);
                    } else {
                        GlideUtils.loadCustomRound(context, item.getPictures().get(2).getThumbnailPath(), ivPic3, 4);
                    }
                }
            }
        } else {
            llOneImg.setVisibility(View.GONE);
            llThreeImg.setVisibility(View.GONE);
            placeholderView.setVisibility(View.GONE);
            spaceView.setVisibility(View.VISIBLE);
            llContentParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            llContent.setLayoutParams(llContentParams);

        }

        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String addressType = "";
                if (Urls.baseUrl.contains("dev_kmer")) {
                    addressType = "1";
                } else {
                    addressType = "0";
                }
                Intent intent = new Intent(context, WebCommonActivity.class);
                String url = "";
                if(!Utils.isNull(item.getQuoteCode())&&"1".equals(item.getQuoteFlag())){
                    url = Urls.tuwenDetail+"mainCode=" + item.getQuoteCode()+"&newsCode=" + item.getNewsCode() + "&token=" + Token.INSTANCE.getToken() + "&statusHeight=" + Utils.getStatusBarHeightDp(context) + "&time=" + System.currentTimeMillis() + "&addressType=" + addressType;
//                    intent.putExtra("title", "详情");
//                    intent.putExtra("isTuwen", 3);
//                    intent.putExtra("resourceId", item.getQuoteCode());
//                    intent.putExtra("newsContent", "");
                }else if("2".equals(item.getQuoteFlag())){
                    url =item.getNewsContent();
                }else{
                    url = Urls.webBaseUrl+"indexNew.html#/newDetail?newsCode=" + item.getNewsCode() + "&token=" + Token.INSTANCE.getToken() + "&statusHeight=" + Utils.getStatusBarHeightDp(context) + "&time=" + System.currentTimeMillis() + "&addressType=" + addressType;
                }
                intent.putExtra("isShare", true);
                intent.putExtra("createEmployeeName", item.getEmployeeName());
                intent.putExtra("resourceId", item.getNewsCode());
                intent.putExtra("newsContent", item.getNewsContent());
                intent.putExtra("url", url);
                intent.putExtra("newsTitle", item.getNewsTitle());
                if (item.getPictures().size() > 0) {
                    if (Utils.isNull(item.getPictures().get(0).getThumbnailPath())) {
                        intent.putExtra("picUrl", item.getPictures().get(0).getPicturePath());
                    } else {
                        intent.putExtra("picUrl", item.getPictures().get(0).getThumbnailPath());
                    }
                }else{
                    intent.putExtra("picUrl","");
                }
                intent.putExtra("waterMark", "13");
                context.startActivity(intent);
                if (!Utils.isNull(item.getId())) {
                    toClickNum(item.getId());
                }
            }
        });

    }

    private void toClickNum(String newsId) {
        Map<String, String> map = new HashMap<>();
        map.put("id", newsId);
        MyHttpRequest.getDefault().postRequestCompat(null, Urls.setClickNum, SignUtils2.makeSignStr(map), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) throws Exception {
            }

            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }
}
